<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

	public function index()
	{
		$this->design();
		$data['jawaban'] = $this->jawaban();
		//$this->load->view('view_main', $data);
		$config['base_url']		 = base_url().'main';
		$data['title']				 = "Automatic Essay Scoring";
		$data['main_content']	 = 'view_main';
		$this->load->view('includes/template_admin', $data);
	}
	
	public function jawaban()
	{
		$this->load->model('model_jawaban');
		if($data = $this->model_jawaban->jawaban()){
			return $data;
		}
	}
	
	public function jawaban_ngram()
	{
		$this->load->model('model_jawaban');
		if($data = $this->model_jawaban->jawaban_ngram()){
			return $data;
		}
	}
	
	public function design(){
		$this->load->view('view_design');
	}
	
	
	//-------N-GRAM METHOD START-------
	public function ngram()
	{
		$data['jawaban'] 		 = $this->jawaban_ngram();
		$data['title'] 				 = "ngram";
		$data['ngram'] 			 = $this->input->post('ngram');
		if($data['ngram']==''){
			$data['ngram']		 = 1;
		}else{
			$data['ngram']		 = $data['ngram'];
		}
		$data['kalimat'] 			 = $this->string_validation_ngram();
		$data['hasil_proses']	 = $this->ngram_method($data['kalimat'], $data['ngram']);
		$this->design();
		$data['main_content']	 = 'view_ngram';
		$this->load->view('includes/template_admin', $data);
	}
	
	public function ngram_method($kalimat,$ngram)
	{
		if($kalimat){
			foreach($kalimat['check_jawaban'] as $data){
				$hasil[$data]	= explode(' ',$kalimat['jawaban_siswa'][$data]);
				$jml[$data]	= count($hasil[$data]);
				if($jml[$data]>=$ngram){
				for($i=0;$i<$jml[$data]-($ngram-1);$i++){
					$kata[$data][$i] = "";
					for($I=$i; $I<$ngram+$i; $I++){
						$kata[$data][$i] = trim($kata[$data][$i])." ".trim($hasil[$data][$I]);
					}
				}
				}else{
					$kata[$data] = null;
				}
			}
			return $kata;
		}
	}
	//-------N_GRAM METHOD END-------
	
	
	//-------TFIDF METHOD START-------
	public function tfidf()
	{
		$data['jawaban'] 		= $this->jawaban();
		$data['title'] 				= "tfidf";
		$this->design();
		
		$data['ngram'] 			= $this->input->post('ngram');
		$data['error_digit'] 		= $this->input->post('error_digit');
		$data['sinonim'] 			= $this->input->post('sinonim');
		$data['kalimat'] 			= $this->string_validation();
		$data['hasil_tfidf_token'] 	= $this->tfidf_token($data['kalimat'], $data['sinonim']);
		$data['ngram'] 			= $this->ngram_validation($data['ngram'], $data['hasil_tfidf_token']['ngram']);
		$data['error_digit'] 		= $this->error_validation($data['error_digit']);
		$data['hasil_proses'] 	= $this->tfidf_method($data['hasil_tfidf_token'], $data['ngram'], $data['error_digit'], $data['sinonim']);
		$data['nilai']				= $this->tfidf_normalisasi($data['hasil_proses']);
		
		//$this->load->view('view_jawaban', $data);
		$data['main_content']	 = 'view_jawaban';
		$this->load->view('includes/template_admin', $data);
	}
	
	public function tfidf_normalisasi($hasil){
		if($hasil){
		$this->load->model('model_fitur');
		$hasil['normal']	= $this->model_fitur->tfidf_normal($hasil);
		$hasil['nilai']		= $this->model_fitur->nilai($hasil);
		}
		return $hasil;
	}
	
	public function tfidf_token($kalimat, $sinonim){
		if($kalimat){
		$this->load->model('model_fitur');
		
		$kunci 				= $this->model_fitur->tfidf_token_kunci($kalimat);
		$jawaban 			= $this->model_fitur->tfidf_token_jawaban($kalimat, $kunci['token'], $sinonim);
		$token['ngram']	= $this->token_ngram($kunci['ngram'], $jawaban['ngram']);
		$token['kunci'] 	= $kunci;
		$token['jawaban'] 	= $jawaban;
		$token['skor']		= $kalimat['skor'];
		
		return $token;
		}
	}
	
	public function token_ngram($kunci, $jawaban){
		if($kunci < $jawaban){$ngram = $kunci;}else{$ngram = $jawaban;}
		return $ngram;
	}
	
	public function error_validation($error){
		if($error!=0){$error = $error;}else{$error = 4;}
		return $error;
	}
	
	public function ngram_validation($ngram, $token_ngram){
		if($ngram<=$token_ngram && $ngram!=0){$ngram = $ngram;}else{$ngram = 1;}
		return $ngram;
	}
	
	public function tfidf_method($kalimat, $ngram, $error_digit, $sinonim){
		if($kalimat){
		$this->load->model('model_fitur');
		$this->load->model('model_tfidf');
		
		$hasil 					= $this->model_fitur->matching_tfidf($kalimat, $ngram, $sinonim);
		$hasil['error_digit'] 	= $error_digit;
		$hasil['df'] 				= $this->model_tfidf->df($hasil);
		$hasil['D/df'] 			= $this->model_tfidf->Ddf($hasil, $hasil['jumlah_kunci']); 
		$hasil['W'] 			= $this->model_tfidf->W($hasil);
		$hasil['bobot_W']	= $this->model_tfidf->bobot_W($hasil);
		$hasil['VSM'] 		= $this->model_tfidf->VSM($hasil);
		$hasil['kkdoc'] 		= $this->model_tfidf->kkdoc($hasil);
		$hasil['sqrt'] 			= $this->model_tfidf->sqrt($hasil);
		$hasil['cosin'] 		= $this->model_tfidf->cosin($hasil);
		$hasil['skor']			= $kalimat['skor'];
		
		return $hasil;
		}
	}
	//-------TFIDF METHOD END-------
	
	
	//-------BLUE METHOD START-------
	public function bleu(){
		$data['jawaban']	 	= $this->jawaban();
		$data['title'] 			= "bleu";
		$this->design();
		
		$data['error_digit']		= $this->input->post('error_digit');
		$data['sinonim'] 			= $this->input->post('sinonim');
		$data['kalimat'] 			= $this->string_validation();
		$data['hasil_tfidf_token'] 	= $this->bleu_token($data['kalimat'], $data['sinonim']);
		$data['error_digit']		= $this->error_validation($data['error_digit']);
		$data['hasil_proses']	= $this->bleu_method($data['hasil_tfidf_token'], $data['error_digit'], $data['sinonim']);
		$data['nilai']				= $this->bleu_normalisasi($data['hasil_proses']);
		
		//$this->load->view('view_jawaban', $data);
		$data['main_content']	 = 'view_jawaban';
		$this->load->view('includes/template_admin', $data);
	}
	
	public function bleu_normalisasi($hasil){
		if($hasil){
		$this->load->model('model_fitur');
		$hasil['normal']	= $this->model_fitur->bleu_normal($hasil);
		$hasil['nilai']		= $this->model_fitur->nilai($hasil);
		}
		return $hasil;
	}
	
	public function bleu_token($kalimat, $sinonim){
		if($kalimat){
		$this->load->model('model_fitur');
		$hasil = $this->model_fitur->bleu_token_kalimat($kalimat, $sinonim);
		return $hasil;
		}
	}
	
	public function bleu_method($kalimat, $error_digit, $sinonim){
		if($kalimat){
		$kalimat['error_digit'] 	= $error_digit;
		$kalimat['e']				= 2.71828;
		$kalimat['skor']			= $kalimat['skor'];
		
		$this->load->model('model_fitur');
		$this->load->model('model_bleu');
		
		$hasil = $this->model_fitur->matching_bleu($kalimat, $sinonim);
		$hasil = $this->model_bleu->hitung_bleu($hasil);
		
		return $hasil;
		}
	}
	//-------BLEU METHOD END-------
	
	
	//-------TF-ICF METHOD-------
	public function tficf()
	{
		$data['jawaban'] = $this->jawaban();
		
		$data['title'] = "tficf";
		$this->design();
		
		$data['kalimat'] = $this->tfidf_validation();
		if($data['kalimat']){
			$this->load->view('view_jawaban', $data);
		}
		else{
			$this->load->view('view_jawaban', $data);
		}
	}
	
	
	//-------------STRING VALIDATION--------------//
	public function string_validation()
	{
		$hasil = array();
		$this->load->library('form_validation');
		$this->form_validation->set_rules('check_jawaban', 'check_jawaban', 'required');
		$this->form_validation->set_rules('check_kunci', 'check_kunci', 'required');
		
		if($this->form_validation->run()){
			foreach($this->input->post('check_jawaban') as $value){
				$hasil_jawaban_siswa[$value] 	= $this->input->post('data_jawaban'.$value);
				$check_jawaban[$value] 			= $value;
				$skor[$value]					= $this->input->post('skor'.$value);
			}
			foreach($this->input->post('check_kunci') as $value){
				$hasil_kunci[$value] 	= $this->input->post('data_kunci'.$value);
				$check_kunci[$value] 	= $value;
				$skor[$value]		= $this->input->post('skor'.$value);
			}
			$hasil['kunci'] 		= $hasil_kunci;
			$hasil['jawaban_siswa'] = $hasil_jawaban_siswa;
			$hasil['check_jawaban'] = $check_jawaban;
			$hasil['check_kunci'] 	= $check_kunci;
			$hasil['skor']			= $skor;
		}
		
		return $hasil;
	}
	
	public function string_validation_ngram()
	{
		$hasil = array();
		$this->load->library('form_validation');
		$this->form_validation->set_rules('check_jawaban', 'check_jawaban', 'required');
		
		if($this->form_validation->run()){
			foreach($this->input->post('check_jawaban') as $value){
				$hasil_jawaban_siswa[$value] 	= $this->input->post('data_jawaban'.$value);
				$check_jawaban[$value] 			= $value;
			}
			$hasil['jawaban_siswa'] = $hasil_jawaban_siswa;
			$hasil['check_jawaban'] = $check_jawaban;
		}
		
		return $hasil;
	}
	
	public function matching($S1, $S2, $suku)
	{
		$S1 = explode(" ", trim($S1));
		$S2 = explode(" ", trim($S2));
		$jml_S1 = count($S1);
		$jml_S2 = count($S2);
		$kunci_check = array();
		$jawaban_check = array();
		
		$matc = 0;
		
		for($i=0; $i<$jml_S1-($suku-1); $i++)
		{
			$kunci_check[$i] = "";
			for($I=$i; $I<$suku+$i; $I++)
			{
				$kunci_check[$i] = trim($kunci_check[$i])." ".trim($S1[$I]);
			}
			for($j=0; $j<$jml_S2-($suku-1); $j++)
			{
				$jawaban_check[$j] = "";
				for($J=$j; $J<$suku+$j; $J++)
				{
					$jawaban_check[$j] = trim($jawaban_check[$j])." ".trim($S2[$J]);
				}
				if(trim($kunci_check[$i])==trim($jawaban_check[$j]))
				{
					$matc++;
				}
			}
		}	
		return $matc;
	}
	
}
